//package com.xb.soft.controller.xbqrmaker;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.xb.soft.model.DeviceQrcodeImage;
//import com.xb.soft.service.DeviceQrcodeImageService;
//import com.xb.soft.util.file.zip.FileZipUtil;
//import com.xb.soft.util.file.zip.ZipUtils;
//import com.xb.soft.util.qrcode.utils.Convert;
//import com.xb.soft.util.qrcode.utils.DesUtil;
//import com.xb.soft.util.qrcode.utils.QrCodeEncoderHandler;
//import com.xb.soft.util.qrcode.utils.StringUtil;
//import com.xb.soft.util.qrcode.utils.qartutil.DateUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.util.StringUtils;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.*;
//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.Paths;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
///**
// * @author zchcpy
// */
//@Slf4j
//@Controller
//@RequestMapping("/qrmaker")
//public class Xbqrmaker_back201217 {
//  @Resource
//  private DeviceQrcodeImageService deviceQrcodeImageService;
//  private static final String UPLOAD_PATH = "File/image/upload";
//  @RequestMapping("/index")
//  public ModelAndView index(@RequestParam("id") int id, @RequestParam("from") String from,
//                            //
//                            HttpServletRequest request, HttpServletResponse response, Model model) {
//    log.info("index mode:" + model);
//    return new ModelAndView("javaQr/qrMake");
//  }
//  /**
//   * String contents = "http://" + serverUrl + "/qrdt.do?dsn=" + selectDeviceSn;
//   * String contents = "http://" + serverUrl + "/q.do?v=2&s=" + i;
//   *
//   * @param qrSize         int
//   * @param colorContent   int
//   * @param colorBg        int
//   * @param correctLevel   int
//   * @param imgFormat      String
//   * @param serverUri      String
//   * @param colorBorder    int
//   * @param logoPath       String
//   * @param begindeviceNum int
//   * @param enddeviceNum   int
//   * @param versionNumber  String
//   * @param devicename     int
//   * @param request        HttpServletRequest
//   * @param response       HttpServletResponse
//   * @param model          Model
//   * @return Map
//   */
//  @ResponseBody
//  @RequestMapping("/make")
//  public Map make(@RequestParam("qrSize") int qrSize, @RequestParam("colorContent") int colorContent,/*分行*/
//                  @RequestParam("colorBg") int colorBg, @RequestParam("correctLevel") int correctLevel,/*分行*/
//                  @RequestParam("imgFormat") String imgFormat, @RequestParam("serverUri") String serverUri,/*分行*/
//                  @RequestParam("colorBorder") int colorBorder, @RequestParam("logoPath") String logoPath,/*分行*/
//                  @RequestParam("begindeviceNum") int begindeviceNum, @RequestParam("enddeviceNum") int enddeviceNum,/*分行*/
//                  @RequestParam("versionNumber") String versionNumber, @RequestParam("devicename") int devicename,/*分行*/
//                  HttpServletRequest request, HttpServletResponse response, Model model) {
//    HashMap<Object, Object> map = new HashMap<>();
//    try {
//      String[] split = serverUri.split("=");
//      String URL = split[1];
//      String equalUrl = split[0];
//      QrCodeEncoderHandler handler = new QrCodeEncoderHandler();
//      long currentTimeMillis = System.currentTimeMillis();
//      String property = System.getProperty("user.dir");
//      String thisTimePath = UPLOAD_PATH + "/" + DateUtil.getPathdate() + currentTimeMillis + "_B" + begindeviceNum + "_E" + enddeviceNum + "_" + StringUtil.getTimeWithPureNumber() + "/";
//      for (int i = begindeviceNum; i <= enddeviceNum; i++) {
//        String sn = Convert.intToHexString(i, 16).toUpperCase();
//        String selectDeviceSn = "";
//        String qrContent = "";
//        selectDeviceSn = DesUtil.encrypt(sn);
//        System.out.println("selectDeviceSn = " + selectDeviceSn);
//        if ("V1".equals(versionNumber)) {
//          qrContent = "http://" + URL + "/qrdt.do?dsn=" + selectDeviceSn;
//        }
//        if ("V2".equals(versionNumber)) {
//          qrContent = "http://" + URL + "/q.do?v=2&s=" + i;
//        }
//        if ("V3".equals(versionNumber)) {
//          qrContent = "http://" + URL + "/qrdt.do?dsn=" + i;
//        }
//        StringBuffer requestUrl = request.getRequestURL();
//        String requestUri = request.getRequestURI();
//        //网络地址 String url = "/static/img/qr/";
//        String replace = requestUrl.toString().replace(requestUri, "");
//        String httpurl = replace + "/" + +i + "." + imgFormat;
//        System.out.println("httpurl = " + httpurl);
//        //本机硬盘位置
//        //String filePathRoot = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
//        //String filePathRoot = request.getSession().getServletContext().getRealPath();
//        //System.out.println("filePathRoot = " + filePathRoot);
//        Path directory = Paths.get(thisTimePath);
//        //判断是否存在该文件夹，若不存在则创建文件夹
//        if (!Files.exists(directory)) {
//          Files.createDirectories(directory);
//        }
//        String hardDiskUrl = property + "/" + thisTimePath + i + "." + imgFormat;
//        String replace1 = hardDiskUrl.replace("\\", "/");
//        handler.encode(qrContent, qrSize, qrSize, hardDiskUrl, null, colorContent, colorBg, i, imgFormat, devicename);
//        DeviceQrcodeImage deQrImg = new DeviceQrcodeImage();
//        deQrImg.setDate(StringUtil.getNowTimeFortest());
//        deQrImg.setDeviceId(String.valueOf(i));
//        deQrImg.setDeviceNun(sn);
//        deQrImg.setDeviceSn(selectDeviceSn);
//        deQrImg.setHardDiskUrl(replace1);
//        deQrImg.setQrcodeVisitUrl(qrContent);
//        deQrImg.setHttpUrl(httpurl);
//        deQrImg.setEqualUrl(Integer.valueOf(equalUrl));
//        deQrImg.setVersionNumber(versionNumber);
//        deviceQrcodeImageService.insertSelective(deQrImg);
//      }
//    }
//    catch (Exception e1) {
//      e1.printStackTrace();
//    }
//    map.put("name", "二维码生成完毕");
//    return map;
//  }
//  /**
//   * @param request  HttpServletRequest
//   * @param response HttpServletResponse
//   * @param model    Model
//   * @return ModelAndView
//   */
//  @RequestMapping("/qrDownloadBatch")
//  public ModelAndView qrDownloadBatch(HttpServletRequest request, HttpServletResponse response, Model model) {
//    ModelAndView modelAndView = new ModelAndView("javaQr/qrDownloadBatch");
//    log.info("modelAndView:" + modelAndView);
//    return modelAndView;
//  }
//  /**
//   * List<DeviceQrcodeImage>
//   *
//   * @param begindeviceNum Integer
//   * @param enddeviceNum   Integer
//   * @param equalUrl       Integer
//   * @param request        request
//   * @param response       HttpServletResponse
//   * @param model          Model
//   */
//  @ResponseBody
//  @RequestMapping("/query")
//  public String query(Integer begindeviceNum, Integer enddeviceNum, Integer equalUrl,
//                      //
//                      HttpServletRequest request, HttpServletResponse response, Model model) {
//    // PrintWriter p = null;
//    try {
//      System.out.println("begindeviceNum = " + begindeviceNum);
//      System.out.println("enddeviceNum = " + enddeviceNum);
//      System.out.println("equalUrl = " + equalUrl);
//      System.out.println("model = " + model);
//      // p = response.getWriter();
//      List<DeviceQrcodeImage> deviceQrcodeImageList = deviceQrcodeImageService.selectBydevice(begindeviceNum, enddeviceNum, equalUrl);
//      for (DeviceQrcodeImage deviceQrcodeImage : deviceQrcodeImageList) {
//        System.out.println("deviceQrcodeImage = " + deviceQrcodeImage);
//        break;
//      }
//      model.addAttribute("deviceQrcodeImageList", deviceQrcodeImageList);
//      // p.print(deviceQrcodeImageList);
//      String returnJsonStr = JSON.toJSONString(deviceQrcodeImageList);
//      JSONArray returnJsonArray = JSON.parseArray(returnJsonStr);
//      System.out.println("returnJsonArray = " + returnJsonArray);
//      return returnJsonArray.toJSONString();
//    }
//    catch (Exception e) {
//      log.error(e.getMessage(), e);
//      return "-1";
//    }
//  }
//  @RequestMapping("/file/download")
//  public void testDownload(HttpServletRequest request, HttpServletResponse response, Model model) {
//    String begindeviceNumStr = request.getParameter("begindeviceNum");
//    String enddeviceNumStr = request.getParameter("enddeviceNum");
//    String equalUrlStr = request.getParameter("equalUrl");
//    Integer begindeviceNum = null;
//    Integer enddeviceNum = null;
//    int equalUrl = 0;
//    if (!StringUtils.isEmpty(begindeviceNumStr)) {
//      begindeviceNum = Integer.parseInt(begindeviceNumStr);
//    }
//    if (!StringUtils.isEmpty(enddeviceNumStr)) {
//      enddeviceNum = Integer.parseInt(enddeviceNumStr);
//    }
//    if (!StringUtils.isEmpty(equalUrlStr)) {
//      equalUrl = Integer.parseInt(equalUrlStr);
//    }
//    List<DeviceQrcodeImage> deviceQrcodeImageList = deviceQrcodeImageService.selectBydevice(begindeviceNum, enddeviceNum, equalUrl);
//    for (DeviceQrcodeImage deviceQrcodeImage : deviceQrcodeImageList) {
//      System.out.println("deviceQrcodeImage = " + deviceQrcodeImage.getHardDiskUrl());
//    }
//    int size = deviceQrcodeImageList.size();
//    System.out.println("共有size = " + size + "个文件.");
//    String beginDeviceId = deviceQrcodeImageList.get(0).getDeviceId();
//    String endDeviceId = deviceQrcodeImageList.get(deviceQrcodeImageList.size() - 1).getDeviceId();
//    DeviceQrcodeImage deviceQrcodeImage = deviceQrcodeImageList.get(0);
//    String hardDiskUrl = deviceQrcodeImage.getHardDiskUrl();
//    String[] hardDiskUrlSplitStr = hardDiskUrl.split("/");
//    int hardDiskUrlSplitStrLength = hardDiskUrlSplitStr.length;
//    System.out.println("hardDiskUrlSplitStrLength = " + hardDiskUrlSplitStrLength);
//
//    String dayOfHardDiskUrl = hardDiskUrlSplitStr[hardDiskUrlSplitStrLength - 2];
//    String doIndexDayOfHardDiskUrl = "/" + dayOfHardDiskUrl + "/";
//    int dayIndexOfHardDiskUrl = hardDiskUrl.indexOf(doIndexDayOfHardDiskUrl);
//    String dayDicUrl = hardDiskUrl.substring(0, dayIndexOfHardDiskUrl) + "/";
//
//    String dayOfHardDiskUrlToSave = hardDiskUrlSplitStr[hardDiskUrlSplitStrLength - 3];
//    String doIndexDayOfHardDiskUrlToSave = "/" + dayOfHardDiskUrlToSave + "/";
//    int dayIndexOfHardDiskUrlToSave = hardDiskUrl.indexOf(doIndexDayOfHardDiskUrlToSave);
//    String dayDicUrlToSave = hardDiskUrl.substring(0, dayIndexOfHardDiskUrlToSave) + "/";
//
//    // 获取本系统所在路径
//    //    String property = System.getProperty("user.dir");
//    //    System.out.println("property = " + property);
//    //    int length = property.length();
//    //    System.out.println("length = " + length);
//    //    int i = length + 1;
//    //    System.out.println("i = " + i);
//    //    System.out.println("hardDiskUrl = " + hardDiskUrl + " " + hardDiskUrl.length());
//    //    String substring = hardDiskUrl.substring(0, length + 42);
//    //    System.out.println("substring = " + substring);
//    //    String sub = substring.substring(i, i + 4);
//    //    System.out.println("sub = " + sub);
//    if (true) {
//      // return;
//    }
//    FileOutputStream fos1 = null;
//    //待下载文件名
//    String fileName = "xb-device-qr-" + beginDeviceId + "-" + endDeviceId + ".zip";
//    // String fileFullName = dayDicUrl + fileName;
//    String fileFullName = dayDicUrlToSave + fileName;
//    try {
//      fos1 = new FileOutputStream(fileFullName);
//    }
//    catch (FileNotFoundException e) {
//      e.printStackTrace();
//    }
//    ZipUtils.toZip(dayDicUrl, fos1, true);
//    //删除文件及文件夹
//    // deviceQrcodeImageService.deleteLIKEhttpurl(sub);
//    // delFile(new File(property + "/" + UPLOAD_PATH));
//    //设置为png格式的文件
//    response.setHeader("content-type", "tapplication/octet-stream");
//    response.setContentType("application/octet-stream");
//    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
//    byte[] buff = new byte[4096];
//    //创建缓冲输入流
//    BufferedInputStream bis = null;
//    OutputStream outputStream = null;
//    try {
//      outputStream = response.getOutputStream();
//      //这个路径为待下载文件的路径
//      bis = new BufferedInputStream(new FileInputStream(fileFullName));
//      int read = bis.read(buff);
//      //通过while循环写入到指定了的文件夹中
//      while (read != -1) {
//        outputStream.write(buff, 0, buff.length);
//        outputStream.flush();
//        read = bis.read(buff);
//      }
//    }
//    catch (IOException e) {
//      //出现异常返回给页面失败的信息
//      e.printStackTrace();
//    }
//    finally {
//      if (bis != null) {
//        try {
//          bis.close();
//        }
//        catch (IOException e) {
//          e.printStackTrace();
//        }
//      }
//      if (outputStream != null) {
//        try {
//          outputStream.close();
//          //          File file = new File("d:/" + fileName + ".zip");
//          //          boolean deleteResult = file.delete();
//          //          log.info(file + " delete result:" + deleteResult);
//        }
//        catch (IOException e) {
//          e.printStackTrace();
//        }
//      }
//    }
//  }
//  public static void main(String[] args) {
//    //    String ss = "C:/Users/Administrator/Desktop/qs/File/image/upload";
//    //    ss = "C:\\Users\\Administrator\\Desktop\\qs\\File\\image";
//    //    boolean b = delFile(new File("C:\\Users\\Administrator\\Desktop\\qs\\File\\image"));
//    testFileZip();
//  }
//  /**
//   * 删除文件
//   *
//   * @param file File
//   * @return boolean
//   */
//  static boolean delFile(File file) {
//    if (!file.exists()) {
//      return false;
//    }
//    if (file.isDirectory()) {
//      File[] files = file.listFiles();
//      if (files != null) {
//        for (File f : files) {
//          delFile(f);
//        }
//      }
//    }
//    boolean delete = file.delete();
//    if (!delete) {
//      //系统进行资源强制回收
//      System.gc();
//      return file.delete();
//    }
//    return delete;
//  }
//  /**
//   * 测试压缩 可行 但是下载却为空 不知为何 准备处理FTP
//   */
//  public static void testFileZip() {
//    FileOutputStream fos1 = null;
//    //待下载文件名
//    String waitToZipDic1 = "F:/zch/code/tools/tools-springboot-soft/File/image/upload/2020/12/17/";
//    //    try {
//    //      fos1 = new FileOutputStream(waitToZipDic1 + "1.zip");
//    //    }
//    //    catch (FileNotFoundException e) {
//    //      e.printStackTrace();
//    //    }
//    // ZipUtils.toZip(waitToZipDic1, fos1, true);
//    List<String> waitToZipFileList = new ArrayList<>();
//    waitToZipFileList.add(waitToZipDic1);
//    FileZipUtil.compress(waitToZipFileList, waitToZipDic1 + "2.zip");
//  }
//}
//
